package com.longxian.shardingjdbcdemo.serviceImpl;
import com.longxian.shardingjdbcdemo.entity.User;
import com.longxian.shardingjdbcdemo.mapper.UserRespoistory;
import com.longxian.shardingjdbcdemo.service.UserService;
import com.longxian.shardingjdbcdemo.util.ResponseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


@Service
public class UserviceImp implements UserService {

	@Resource
	private UserRespoistory userRespoistory;

	@Autowired
	private ResponseUser responseUser;

	/**
	 * 添加user
	 * @param user
	 * @return
	 */
	public ResponseUser addUser(User user) {
		int count=userRespoistory.addUser(
				user.getId(),user.getName(),user.getAge(),user.getCreate_time());
		if(count==0)
			responseUser.setAll(1,"添加失败",0);
		else
			responseUser.setAll(0,"添加成功",count);
		return responseUser;
	}


	/**
	 * 得到所有user
	 * @return
	 */
	public ResponseUser getAllUsers() {
		List<User> users= userRespoistory.getAllUsers();
		if(users.size()==0)
			responseUser.setAll(1,"查询失败",null);
		else
			responseUser.setAll(0,"查询成功",users);
		return responseUser;
	}

	public ResponseUser getUserListByCreateTime(String createTime){
		List<User> users= userRespoistory.getUserListByCreateTime(createTime);
		if(users.size()==0)
			responseUser.setAll(1,"查询失败",null);
		else
			responseUser.setAll(0,"查询成功",users);
		return responseUser;
	}

	/**
	 * 根据用户ID查询用户信息
	 * @return
	 */
	public ResponseUser getUserById(Integer id) {
		User user = userRespoistory.getUserById(id);
		if(null == user)
			responseUser.setAll(1,"查询失败",null);
		else
			responseUser.setAll(0,"查询成功",user);
		return responseUser;
	}

}